package com.ljkj.cordial.chat.bean;

import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.SparseArray;

import com.ljkj.cordial.chat.base.ChatApp;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.io.Serializable;

public class TextCharSequenceTran implements PropertyConverter<TextCharSequenceTran, String>,Serializable {
    static final long serialVersionUID = 42L;
    public static TextCharSequenceTran create(SpannableStringBuilder sequence) {
        TextCharSequenceTran tran = new TextCharSequenceTran();
        tran.setSequence(sequence);
        return tran;
    }

    private SpannableStringBuilder sequence;

    public SpannableStringBuilder getSequence() {
        return sequence;
    }

    public void setSequence(SpannableStringBuilder sequence) {
        this.sequence = sequence;
    }

    public void setTextTranSpannable(String text) {
        SpannableStringBuilder sequence = new SpannableStringBuilder(text);
        setSequence(sequence);
    }

    @Override
    public TextCharSequenceTran convertToEntityProperty(String databaseValue) {
        if (TextUtils.isEmpty(databaseValue)) {
            return null;
        }
        SparseArray<String> array = EmojiPackage.encodeEmoj(ChatApp.getInstance(), databaseValue);
        if (array == null) {
            SpannableStringBuilder span = new SpannableStringBuilder(databaseValue);
            return TextCharSequenceTran.create(span);
        } else {
            SpannableStringBuilder text = EmojiPackage.decodeEmoji(ChatApp.getInstance(), array, databaseValue);
            return TextCharSequenceTran.create(text);

        }
    }

    @Override
    public String convertToDatabaseValue(TextCharSequenceTran entityProperty) {
        if (entityProperty == null) {
            return null;
        }
        return entityProperty.getSequence().toString();
    }
}
